<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\OriginForge;
use app\airobot\model\OriginUserSpin;
use app\airobot\model\Product;
use app\airobot\model\UserModel;
use think\admin\Controller;

/**
 * 超管系统-Spin管理
 * Class UserSpin
 * @package app\airobot\controller
 */
class UserSpin extends Controller
{
    private $table = 'user_spin';
    public $commonLogic;

    /**
     * Spin列表
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = 'Spin列表';
        $query = $this->_query($this->table);
        $where = ["battle_id" => 0];
        if (!empty($_GET['user_id'])) {
            $where['user_id'] = $_GET['user_id'];
        }
        if (!empty($_GET['spin_type'])) {
            $where['spin_type'] = $_GET['spin_type'];
        }
        if (!empty($_GET['status'])) {
            $where['status'] = $_GET['status'];
        }
        $query->where($where);
        $query->orderRaw("FIELD(status, '2', '3', '4','1')")->order(['id' => 'desc'])->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        // 用户
        $userIds = array_column($data, 'user_id');
        $userModel = new UserModel();
        $userList = $userModel->queryList([], ['field' => 'id', 'data' => $userIds], [], [], [], "", $field = "*", $order = "");
        $userMap = array_column($userList, null, 'id');

        // 商品
        $productIds = array_column($data, 'product_id');
        $productModel = new Product();
        $productList = $productModel->queryList([], ['field' => 'id', 'data' => $productIds], [], [], [], "", $field = "*", $order = "");
        $productMap = array_column($productList, null, 'id');

        foreach ($data as &$vo) {
            $vo['username'] = isset($userMap[$vo['user_id']]) ? $userMap[$vo['user_id']]['username'] : '';
            $vo['product_name'] = isset($productMap[$vo['product_id']]) ? $productMap[$vo['product_id']]['name'] : '';
            if (strlen($vo['product_name']) > 30) {
                $vo['product_name'] = $this->commonLogic->TruncateString($vo['product_name'], 30) . "...";
            }

            if ($vo['status'] == 1) {
                $systemProfit = (float)$vo['spin_amount'];
            } else {
                $systemProfit = (float)$vo['spin_amount'] - (float)$vo['product_amount'];
            }
            $vo['system_profit'] = $this->commonLogic->DealAmount(floatval($systemProfit));
            $vo['spin_amount'] = $this->commonLogic->DealAmount($vo['spin_amount']);
            $vo['product_amount'] = $this->commonLogic->DealAmount($vo['product_amount']);
        }
    }

    /**
     * 查看参数
     * @auth true
     */
    public function param()
    {
        $userSpinModel = new \app\airobot\model\UserSpin();
        $vo = $userSpinModel->queryInfo(["id" => $_GET["id"]]);

        // 将 JSON 字符串解析为 PHP 对象或数组
        $paramsJson = json_decode($vo['params_json'], true);
        $formattedJsonString = json_encode($paramsJson, JSON_PRETTY_PRINT);
        $vo['params_json'] = $formattedJsonString;

        $this->vo = $vo;
        OriginUserSpin::mForm('param');
    }
}